/**
 * 
 */
package signalprocessing;


import ikeuilib.JButtonWithMenu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;


/**
 * XScaleを操作するコントロールの集まり
 * 
 * @author ikechan
 *
 */
public class PanelControlXScale extends JPanel {
	
	private static final long serialVersionUID = 1L;
	public static final int ScaleAuto_FITCenter=0;
	public static final int ScaleAuto_FITMaxMin=1;
	

	private PanelControlXScaleInterface Parent=null;
	private PanelControlXScaleConfiguration Conf = null;
	
	private JButtonWithMenu btnTruncate;
	
	public PanelControlXScale(PanelControlXScaleConfiguration c){
		super();
		Conf=c;
		
		//JPanel this = new JPanel();
		this.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),Conf.TitleBorder, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		this.setBounds(7, 342, 189, 119);
		
		//panelControlPanelIntegration.add(this);
		
		this.setLayout(null);
		JButton btnXShiftInc = new JButton("");
		btnXShiftInc.setBounds(145, 59, 25, 25);
		this.add(btnXShiftInc);
		btnXShiftInc.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/arrow_right.png")));
		JButton btnXShiftDec = new JButton("");
		btnXShiftDec.setBounds(117, 59, 25, 25);
		this.add(btnXShiftDec);
		btnXShiftDec.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/arrow_left.png")));

		JButton btnXSpanZoomOut = new JButton("");
		btnXSpanZoomOut.setBounds(145, 23, 25, 25);
		this.add(btnXSpanZoomOut);
		btnXSpanZoomOut.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/wave-zoomout.png")));

		JButton btnXSpanZoomIn = new JButton("");
		btnXSpanZoomIn.setBounds(117, 23, 25, 25);
		this.add(btnXSpanZoomIn);
		btnXSpanZoomIn.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/wave-zoomin.png")));
		JButton btnAutoSpan = new JButton("Auto");
		btnAutoSpan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Parent.xSpanFull();
			}
		});
		btnAutoSpan.setBounds(12, 23, 93, 25);
		this.add(btnAutoSpan);
		
		if(Conf.TruncateButton){
			btnTruncate = new JButtonWithMenu();
			btnTruncate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(btnTruncate.isClickedDropDown()){
						JPopupMenu popup=new JPopupMenu();
						JMenuItem mi = new JMenuItem(" 1.0e-2");
						mi.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								if(Parent!=null){
									Parent.xSpanTruncate(0);
								}
							}

						});
						popup.add(mi);

						JMenuItem mi2 = new JMenuItem("*1.2e-3");
						mi2.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								// 普通は二桁、シフトで１ケタまるめ、
								Parent.xSpanTruncate(1);
							}

						});
						popup.add(mi2);

						JMenuItem mi3 = new JMenuItem(" 1.23e-4");
						mi3.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								if(Parent!=null){
									Parent.xSpanTruncate(2);
								}
							}

						});
						popup.add(mi3);
						popup.show(btnTruncate,0,btnTruncate.getHeight());
					}
					else{
						if(Parent!=null){
							Parent.xSpanTruncate(1);
						}
					}
				}
			});
			btnTruncate.setBounds(12, 84, 93, 25);
			btnTruncate.setText("Truncate");
			add(btnTruncate);
		}
		
		

		btnXSpanZoomIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.ZoomIn;//-20;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.ZoomInKeyShift; //-90;
				}
				Parent.xSpanZoom(r);
			}
		});
		btnXSpanZoomOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.ZoomOut; // 20;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.ZoomOutKeyShift; //90;
				}
				Parent.xSpanZoom(r);
			}
		});
		btnXShiftDec.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.SlideN;  //-20;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.SlideNKeyShift;//-100;
				}
				Parent.xShift(r);
			}
		});
		btnXShiftInc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.SlideP; //20;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.SlidePKeyShift;// 100;
				}
				Parent.xShift(r);
			}

		});

	}
	
	/**
	 * 
	 * @param p
	 */
	public void addParent(PanelControlXScaleInterface p){
		Parent = p;
	}
}
